package com.nb.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.nb.domain.Category;
import com.nb.domain.Dish;
import com.nb.dto.DishDto;
import com.nb.result.Result;
import com.nb.service.CategoryService;
import com.nb.service.DishFlavorService;
import com.nb.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;


@RestController
@Slf4j
@RequestMapping("/dish")
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    CategoryService categoryService;


    //分页器
    @GetMapping("/page")
    public Result<Page> page(int page, int pageSize, String name){
        //分页构造器
        Page<Dish> pageInfo = new Page<>(page,pageSize);
        Page<DishDto> dishDtoPage = new Page<>();
        //条件构造器
        LambdaQueryWrapper<Dish> lqw= new LambdaQueryWrapper<>();
        //过滤条件 当name不为空时
        lqw.like(StringUtils.isNotEmpty(name),Dish::getName,name);
        //排序条件
        lqw.orderByDesc(Dish::getUpdateTime);
        //查询
        dishService.page(pageInfo,lqw);
        //将对象拷贝给dto  除了records字段
        BeanUtils.copyProperties(pageInfo,dishDtoPage,"records");
        //单独处理record
        List<Dish> records = pageInfo.getRecords();
        //对list进行处理，将list中的分类id全部查出名字，给dto赋值
        List<DishDto> list = records.stream().map((item)->{
            DishDto dishDto = new DishDto();
            BeanUtils.copyProperties(item, dishDto);
            Long categoryId = item.getCategoryId();
            Category category = categoryService.getById(categoryId);
            String name1 = category.getName();
            dishDto.setCategoryName(name1);
            return dishDto;
        }).collect(Collectors.toList());
        dishDtoPage.setRecords(list);
        return Result.success(dishDtoPage);
    }


    //新增菜品
    @PostMapping
    public Result<String> addDish(@RequestBody Dish dish){
        dishService.save(dish);
        return Result.success("菜品添加成功！");
    }

    //修改菜品的数据回显
    @GetMapping("/{id}")
    public  Result<DishDto> get(@PathVariable Long id){

        //查询口味信息
        DishDto dto = dishService.getFlavorById(id);

        return Result.success(dto);
    }

    @PutMapping
    public Result<String> update(@RequestBody DishDto dishDto){
        dishService.saveWithFlavor(dishDto);
        return Result.success("菜品添加成功！");
    }

    @GetMapping("/list")
    public Result<List<Dish>> list(Dish dish){

        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getCategoryId()!=null,Dish::getCategoryId,dish.getCategoryId());

        //查询状态为（1）起售的菜品
        lqw.eq(Dish::getStatus,1);
        //排序条件
        lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime);

        //结果
        List<Dish> list = dishService.list(lqw);

        return Result.success(list);
    }

}
